<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/15
 * Time: 13:50
 */
namespace app\index\controller;
use think\Controller;
use think\Db;
class Login extends Controller
{
    /**
     * 登录页面
     * @return mixed
     */
    public function indexLogin(){
        return $this->fetch('login/index_login');
    }
    /**
     * 用户协议
     * @return mixed
     */
    public function goxieyi(){
        $xieyi = Db::name('about_us')
            ->where('about_id',1)->value('agreement');
        return $this->fetch('login/xieyi',['xieyi'=>$xieyi]);
    }

    /**
     * 登录操作
     * @return \think\response\Json
     */
    public function doLogin(){
        extract(input());
        $user = Db::table('up_u_user')
            ->where('user_phone',$phone)
            ->field('user_id, user_username, user_image, user_phone, user_password')
            ->find();
        if(empty($user)){
            return json(['code'=>-1,'msg'=>'该手机号尚未注册']);
        }
        if($user['user_password'] != md5($password)){
            return json(['code'=>-3,'msg'=>'密码错误']);
        }
        session('user_id',$user['user_id']);//用户id
        session('user_name',$user['user_username']);//用户名称
        session('user_thumb',$user['user_image']);//用户头像
        return json(['code' => 200, 'url' => url('index/Index/index'), 'msg' => '登录成功！']);
    }

    /**
     * 找回密码
     * @return mixed|\think\response\Json
     */
    public function resetPwd(){
        if(request()->isAjax ()){
            extract(input());
            $user = Db::table('up_u_user')
                ->where('user_phone',$phone)
                ->field(['user_id','user_phone'])
                ->find();
            if(empty($user)){
                return json(['code'=>-1,'msg'=>'该手机号尚未注册']);
            }
            $d = Db::table('up_phonecode')->where(['phone'=>$phone,'status'=>0])->find();
            if(empty($d)){
                return json(['code'=>-2,'msg'=>'请先发送验证码']);
            }
            $t = time() - $d['time2'];
            if($t >= 600){
                return json(['code'=>-2,'msg'=>'验证码失效']);
            }
            if($code != $d['code']){
                return json(['code'=>-2,'msg'=>'验证码错误']);
            }
            // 启动事务
            Db::startTrans();
            try{
                Db::table('up_phonecode')->where(['phone'=>$phone])->setField ('status',1);
                Db::table('up_u_user')->where(['user_phone'=>$phone])->update(['user_password'=>md5($pwd),'user_update_time'=>date('Y-m-d H:i:s')]);
                // 提交事务
                Db::commit();
                return json(['code'=>200,'url'=>url('index/Login/indexLogin'),'msg'=>'密码重置成功']);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code'=>100,'msg'=>'密码重置失败']);
            }
        }
        return $this->fetch ('login/reset_pwd');
    }

    /**
     * 快捷登录
     * @return mixed|\think\response\Json
     */
    public function quickLogin(){
        extract(input());
        $user = Db::table('up_u_user')
            ->where('user_phone',$phone)
            ->field('user_id, user_username, user_image, user_phone user_password')
            ->find();
        if(empty($user)){
            return json(['code'=>-1,'msg'=>'该手机号尚未注册']);
        }
        $d = Db::table('up_phonecode')->where(['phone'=>$phone,'status'=>0])->find();
        if(empty($d)){
            return json(['code'=>-2,'msg'=>'请先发送验证码']);
        }
        $t = time() - $d['time2'];
        if($t >= 600){
            return json(['code'=>-2,'msg'=>'验证码失效']);
        }
        if($code != $d['code']){
            return json(['code'=>-2,'msg'=>'验证码错误']);
        }
        // 启动事务
        Db::startTrans();
        try{
            Db::table('up_phonecode')->where(['phone'=>$phone])->setField ('status',1);
            // 提交事务
            Db::commit();
            session('user_id',$user['user_id']);//用户id
            session('user_name',$user['user_username']);//用户名称
            session('user_thumb',$user['user_image']);//用户头像
            return json(['code'=>200,'url' => url('index/Index/index'),'msg'=>'登录成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code'=>100,'msg'=>'登录失败']);
        }
    }

    /**
     * 注册
     * @return mixed|\think\response\Json
     */
    public function register(){
        if(request()->isAjax ()){
            extract(input());
            $user = Db::table('up_u_user')
                ->where('user_phone',$phone)
                ->field(['user_id','user_phone'])
                ->find();
            if(!empty($user)){
                return json(['code'=>-1,'msg'=>'该手机号已注册']);
            }
            $d = Db::table('up_phonecode')->where(['phone'=>$phone,'status'=>0])->find();
            if(empty($d)){
                return json(['code'=>-2,'msg'=>'请先发送验证码']);
            }
            if(empty($nick)){
                return json(['code'=>-2,'msg'=>'请填写昵称']);
            }
            $t = time() - $d['time2'];
            if($t >= 600){
                return json(['code'=>-2,'msg'=>'验证码失效']);
            }
            if($code != $d['code']){
                return json(['code'=>-2,'msg'=>'验证码错误']);
            }
            // 启动事务
            Db::startTrans();
            try{
                Db::table('up_phonecode')->where(['phone'=>$phone])->setField ('status',1);
                $ins = [
                    'user_phone'=>$phone,
                    'user_username'=>$nick,
                    'user_password'=>md5($pwd),
                    'user_time'=>date('Y-m-d H:i:s'),
                ];
                Db::table('up_u_user')->insert($ins);
                // 提交事务
                Db::commit();
                return json(['code'=>200,'url' => url('index/Login/indexLogin'),'msg'=>'注册成功']);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code'=>100,'msg'=>'注册失败']);
            }
        }
        return $this->fetch ('login/register');
    }

    /**
     * 发送验证码
     * @return \think\response\Json
     */
    public function sendCode(){
        extract(input());
        $user = Db::table('up_u_user')
            ->where('user_phone',$phone)
            ->field(['user_id','user_phone'])
            ->find();
        //$type == 1代表登录&找回密码  为2代表注册
        if(empty($user) && $type == 1){
            return json(['code'=>-1,'msg'=>'该手机号尚未注册']);
        }
        if(!empty($user) && $type == 2){
            return json(['code'=>-1,'msg'=>'该手机号已注册']);
        }

        $u = Db::table('up_phonecode')
            ->where('phone',$phone)
            ->find();
        $code = GetRandCode(6);
        $res = YzxSms($code,$phone);
        if($res['code'] == 000000){
            if(!empty($u)){
                Db::table('up_phonecode')->where('phone',$phone)->update(['code'=>$code,'time2'=>time(),'status'=>0]);
            }else{
                Db::table('up_phonecode')->insert(['phone'=>$phone,'code'=>$code,'time2'=>time(),'status'=>0]);
            }
            return json(['code'=>200,'msg'=>'验证码发送成功，请注意查收']);
        }else{
            return json(['code'=>100,'msg'=>'验证码发送失败，请重试']);
        }

    }

    /*
     * 退出登录
     */
    public function loginOut(){
        session(null);
        $this->redirect(url('index/login/indexLogin'));
    }
}